package com.yin97.kotiln.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.lcodecore.tkrefreshlayout.footer.LoadingView
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView
import com.yin97.kotiln.R
import com.yin97.kotiln.RetrofitHelper
import com.yin97.kotiln.bean.FuckGoods
import com.yin97.kotiln.bean.JsonResult
import com.yin97.kotiln.ui.activity.WebViewActivity
import com.yin97.kotiln.ui.adapter.AndroidAdapter
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.fragment_ios.*


class ios_Fragment : Fragment() {
    var mPage = 1
    private lateinit  var  adapter : AndroidAdapter
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater!!.inflate(R.layout.fragment_ios, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView ()
        inithttp(mPage)

    }

    fun  initView (){

        val headerView = SinaRefreshView(activity)
        headerView.setArrowResource(R.drawable.ic_arrow)
        headerView.setTextColor(0xff745D5C.toInt())
        ios_tv_refreshLayout.setHeaderView(headerView)
        val loadingView = LoadingView(activity)
        ios_tv_refreshLayout.setBottomView(loadingView)
        ios_rv_view.layoutManager= LinearLayoutManager(activity);
        adapter= AndroidAdapter(R.layout.android_item, null )
        ios_rv_view.adapter=adapter
        adapter.setOnRecyclerViewItemClickListener { view, i ->
            Log.i("sssssssss",adapter.data[i].url)
            val intent = Intent(activity, WebViewActivity::class.java)
            intent.putExtra("Url",adapter.data[i].url)
            context.startActivity(intent)
        }

        ios_tv_refreshLayout.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout) {
                Handler().postDelayed({ refreshLayout.finishRefreshing()
                    mPage=1
                    var  ov: Observable<JsonResult<List<FuckGoods>>> = RetrofitHelper.instance!!.createGsonHttp().getiOSData(1)
                    ov.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({i ->
                        adapter.setNewData(i.results)
                    })
                }, 2000)
            }

            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout) {
                Handler().postDelayed({ refreshLayout.finishLoadmore()}, 2000)
                mPage=mPage+1
                inithttp(mPage);
            }
        });

    }
    fun  inithttp(page: Int){
        var  ov: Observable<JsonResult<List<FuckGoods>>> = RetrofitHelper.instance!!.createGsonHttp().getiOSData(page)
        ov.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({ i ->
           adapter.addData(i.results)
        })
    }
}
